
<%@page import="com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.dao.orm.DynamicQuery"%>
<%@page import="com.liferay.portlet.documentlibrary.model.DLFileEntry"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.liferay.portlet.documentlibrary.model.DLFolderConstants"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil"%>
<%@page import="com.liferay.portlet.documentlibrary.model.DLFolder"%>
<%@page import="com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<theme:defineObjects/>
<portlet:defineObjects />

<portlet:actionURL name="doAssignUserBasedPermission" var="submitUrl">
</portlet:actionURL>

<portlet:actionURL name="doFetchRoleBasedPermissions" var="getUserPermissionsURL">
</portlet:actionURL>

<%
	DLFolder rootfolder;
	List<DLFolder> subFolders = null;
	List<DLFileEntry> filesList = null;
	List<User> userList = null;
	long myScopeGroupId = themeDisplay.getScopeGroupId();
	
	try{
		/* rootfolder = DLFolderServiceUtil.getFolder(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
		System.out.println("DLFolderConstants.DEFAULT_PARENT_FOLDER_ID = "+DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
		
		subFolders = DLFolderServiceUtil.getFolders(myScopeGroupId, rootfolder.getFolderId(),0,-1,null);
		for(DLFolder list:subFolders){
			DLFolderServiceUtil.getFileEntriesAndFileShortcuts(myScopeGroupId, list.getFolderId(), 0, 0,-1);
			System.out.println("list.getFolderId() = "+list.getFolderId());
		} */

		/*  
		 * Get the Files List -present in D&M...
		 */
		filesList = DLFileEntryLocalServiceUtil.getFileEntries(0, -1);

		/*  
		 * Get the User's List -Except the logged-in User...
		 */
		DynamicQuery userListQuery = DynamicQueryFactoryUtil.forClass(User.class)
		.add(PropertyFactoryUtil.forName("userId").ne(new Long(themeDisplay.getUserId())));
		userList = UserLocalServiceUtil.dynamicQuery(userListQuery);

	} catch (Exception e) {
		e.printStackTrace();
	}
%>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>

<aui:form action="<%=submitUrl.toString() %>" type="post" >

<% int index =1;
for(DLFileEntry list : filesList){  %>
<aui:layout>

<aui:column width="25">
	<aui:input name="fileSelect" id="fileSelect" value="<%=list.getFileEntryId() %>" />
</aui:column>	
	
<% String userSelectJSCall = "checkUserSelected("+list.getFileEntryId()+");" ; %>
<aui:column width="25">
	<aui:select name="userSelect" id='<%="userSelect"+list.getFileEntryId() %>' onchange="<%=userSelectJSCall %>">
		<aui:option value="0">Select User</aui:option>
		<%if(userList!=null){
			for(User ulist:userList){
		%>
		<aui:option value="<%=ulist.getUserId() %>"><%=ulist.getFirstName() %>
		</aui:option>
		<%} }%>
	</aui:select>
</aui:column>

<div id='<%="grantPermissions"+list.getFileEntryId()%>'>
	<aui:column width="15">
		<aui:input name="view" id='<%="view"+list.getFileEntryId() %>' type="checkbox" onchange="onChangeView(this);"/>
	</aui:column>
	
	<aui:column width="15">
		<aui:input name="delete" id="delete" type="checkbox" onchange="onChangeDelete(this);"/>
	</aui:column>
</div>

<%String doSubmitForm = "doSubmitForm("+list.getFileEntryId() +"); " ;	%> 
<aui:column width="20">
	<aui:button name="submit" type="button" value="Save" onClick="<%=doSubmitForm %>"/>
</aui:column>	

</aui:layout>	
	<%} %>
</aui:form>



<script type="text/javascript" >
	
$(document).ready(function(){ 
	
  jQuery( "div[id^='grantPermissions']" ).each(function(){
 	 jQuery(this).css("display", "none");
  });
	 		
});				//ready block end..
	
	
//Submit the form using an Ajax call..	
function doSubmitForm(fileEntryId){
	
//	var userSelected = jQuery("#<portlet:namespace/>userSelect"+fileEntryId+" option:selected").val();
	var userSearchedFor = 'userSelect'+fileEntryId;
	var userSelected = jQuery("select#<portlet:namespace/>"+userSearchedFor+" option:selected").val();
	
	var viewPerms = jQuery("#<portlet:namespace/>view"+fileEntryId+"Checkbox").attr("checked");
	
	jQuery.ajax({
		
		type:"post",
		url:"<%=submitUrl.toString()%>",
		data:"fileSelect="+fileEntryId+"&userSelect="+userSelected+"&viewCheckbox="+viewPerms,
		error:function(){
			alert("Error in Ajax Call-Please check the provided details..!!!");
		},
		success:function(){
		 // Do Nothing
		},
	});
	
}



// fetch repective User-Role based Permisions using an Ajax call..
function checkUserSelected(fileEntryId){
	
	var userSearchedFor = 'userSelect'+fileEntryId;
	var userSelected = jQuery("select#<portlet:namespace/>"+userSearchedFor+" option:selected").val();
		  
	if(userSelected=="0"){
		$("#grantPermissions"+fileEntryId).hide();
	}else{
	//by default uncheck all the checkBox fields  && check them on receiving permissions from ajax response..	
		jQuery("#<portlet:namespace/>view"+fileEntryId+"Checkbox").attr("checked",false);
		
		jQuery.ajax({
			type:"post",
			url: "<%= getUserPermissionsURL.toString()%>",
			data:"fileSelect="+fileEntryId+"&userSelect="+userSelected,
			error:function(){
				alert("Error in Ajax Call-Please check the provided details..!!!");
			},
			success:function(data){
				var resp = data;
				if(resp.search("VIEW")!=-1){
				//	alert("View Permission found ..!!!");
				//	$("#<portlet:namespace/>view"+fileEntryId+"Checkbox").attr("checked",true);
					jQuery("#<portlet:namespace/>view"+fileEntryId+"Checkbox").attr("checked",true);	 
					}

			}
		});
		
	//	fetch permissions via ajax success response and ...check the respective fields in the div[grantPermissions] ../
		$("#grantPermissions"+fileEntryId).show();
	}
}
	
function onChangeView(){		
	var viewChecked = document.getElementById("<portlet:namespace/>viewCheckbox"+fileEntryId).checked;
	alert("viewChecked ==" +viewChecked);
}
	

</script>